package cn.HL.org.service.impl;

import cn.HL.basic.service.impl.BaseServiceImpl;
import cn.HL.basic.util.MD5Utils;
import cn.HL.basic.util.StrUtils;
import cn.HL.org.domain.Employee;
import cn.HL.org.mapper.EmployeeMapper;
import cn.HL.org.service.IEmployeeService;
import cn.HL.user.domain.Logininfo;
import cn.HL.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl   extends BaseServiceImpl<Employee> implements IEmployeeService{
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;
    /**
     * 联级新增
     */
    @Override
    public void save(Employee employee) {
        Logininfo logininfo =employee2Logininfo(employee);
        logininfoMapper.save(logininfo);
        employee.setLogininfo_id(logininfo.getId());
        logininfo.setType(0);
        //盐值
        String salt = StrUtils.getComplexRandomString(32);
        employee.setSalt(salt);
        //设置面密码
        String password = MD5Utils.encrypByMd5(employee.getPassword()+salt);
        employee.setPassword(password);
        employeeMapper.save(employee);
        logininfo.setPassword(password);
        logininfo.setSalt(salt);
        logininfoMapper.update(logininfo);
    }


    /**
     * 联级修改
     */
    @Override
    public void update(Employee employee) {
        Logininfo logininfo =employee2Logininfo(employee);
        employee.setLogininfo_id(logininfo.getId());

        employeeMapper.update(employee);
        logininfoMapper.update(logininfo);
    }

    /**
     * 联级删除
     * @param id
     */
    @Override
    public void removeById(Long id) {
        Employee employee = employeeMapper.loadById(id);
        logininfoMapper.deleteById(employee.getLogininfo_id());
        employeeMapper.deleteById(id);
    }
    /**
     * 联级批量删除
     *
     */
    @Override
    public void batchDelDept(List<Long> ids) {
        List<Long> logininfoIds = new ArrayList<Long>();
        for (Long id:ids){
            logininfoIds.add(employeeMapper.loadById(id).getLogininfo_id());
        }
        employeeMapper.batchDelDept(ids);
        logininfoMapper.batchDelDept(logininfoIds);
    }


    /**
     * 数据的拷贝
     * @return
     */
    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        //拷贝数据只要两个对象里面的有的属性名一样就能拷贝
        BeanUtils.copyProperties(employee,logininfo);
        //设置为管理员
        logininfo.setType(0);
        return logininfo;
    }


    @Override
    public Employee loadShopByLogininfo(Long id) {
        return employeeMapper.loadByLogininfoId(id);
    }

    @Override
    public List<Employee> findByShop(Long shopId) {
        return employeeMapper.findByShop(shopId);

    }
}
